﻿using System;

namespace Songhay
{
    /// <summary>
    /// Static members for type handling.
    /// </summary>
    public static partial class FrameworkType
    {
        /// <summary>
        /// Returns <c>true</c> when the specified value
        /// is an empty array, not an array or null.
        /// </summary>
        /// <param name="frameworkValue">The specified value.</param>
        public static bool ArrayIsNullOrEmptyOrNotArray(object frameworkValue)
        {
            if(frameworkValue == null) return true;
            if(frameworkValue is Array)
            {
                Array array = frameworkValue as Array;
                return (array.Length == 0);
            }
            else
            {
                return true;
            }
        }

        /// <summary>
        /// Converts Unix time stamps
        /// to <see cref="System.DateTime"/>.
        /// </summary>
        /// <param name="unixTime">A <see cref="System.Double"/>.</param>
        /// <returns>A <see cref="System.DateTime"/>.</returns>
        public static DateTime DateTimeFromUnixTime(double unixTime)
        {
            return (new DateTime(1970, 1, 1, 0, 0, 0)).AddSeconds(unixTime);
        }

        /// <summary>
        /// Converts the current time
        /// to a Unix time stamp.
        /// </summary>
        /// <returns>A <see cref="System.Double"/>.</returns>
        public static double DateTimeToUnixTime()
        {
            return (DateTime.UtcNow
                - new DateTime(1970, 1, 1, 0, 0, 0)).TotalSeconds;
        }

        /// <summary>
        /// Converts a <see cref="System.DateTime"/>
        /// to a Unix time stamp.
        /// </summary>
        /// <param name="dateValue">The <see cref="System.DateTime"/>.</param>
        /// <returns>A <see cref="System.Double"/>.</returns>
        public static double DateTimeToUnixTime(DateTime dateValue)
        {
            return (dateValue.ToUniversalTime()
                - new DateTime(1970, 1, 1, 0, 0, 0)).TotalSeconds;
        }

        /// <summary>
        /// Returns <c>true</c> when the specified value
        /// is <c>null</c> or <see cref="System.String.Empty"/>.
        /// </summary>
        /// <param name="frameworkValue">The framework value.</param>
        public static bool ObjectIsNullOrEmptyString(object frameworkValue)
        {
            if(frameworkValue == null) return true;
            else return string.IsNullOrEmpty(frameworkValue.ToString());
        }

        /// <summary>
        /// Returns the conventional database null
        /// (<see cref="System.DBNull"/>)
        /// for Microsoft SQL Server systems.
        /// </summary>
        /// <returns><see cref="System.DBNull"/></returns>
        public static DBNull SqlDatabaseNull()
        {
            return DBNull.Value;
        }
    }
}
